<template>
  <div class="container">
    <div id="slider">
      <div
          v-for="(item, index) in items"
          :key="index"
          class="item"
          :style="{ backgroundImage: `url(${item.image})` }"
          :class="{ active: index === activeIndex }"
      >
        <div class="content">
          <div class="title">{{ item.title }}</div>
          <div class="des">{{ item.description }}</div>
          <button class="btn">Click me</button>
        </div>
      </div>

      <div class="buttons">
        <div class="s_button" @click="prependLastItem">左</div>
        <div class="s_button" @click="appendFirstItem">右</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const items = ref([
  { image: 'src/assets/image/1.jpg', title: 'Hello1 World!', description: 'Hello world! Good luck!' },
  { image: 'src/assets/image/2.jpg', title: 'Hello2 World!', description: 'Hello world! Good luck!' },
  { image: 'src/assets/image/3.jpg', title: 'Hello3 World!', description: 'Hello world! Good luck!' },
  { image: 'src/assets/image/4.jpg', title: 'Hello4 World!', description: 'Hello world! Good luck!' },
  { image: 'src/assets/image/5.jpg', title: 'Hello5 World!', description: 'Hello world! Good luck!' },
  { image: 'src/assets/image/6.jpg', title: 'Hello6 World!', description: 'Hello world! Good luck!' },
]);

const activeIndex = ref(0);

const prependLastItem = () => {
  if (items.value.length > 0) {
    const lastItem = items.value.pop();
    items.value.unshift(lastItem);
  }
};

const appendFirstItem = () => {
  if (items.value.length > 0) {
    const firstItem = items.value.shift();
    items.value.push(firstItem);
  }
};
</script>

<style scoped>
body {
  background-color: #fafafa;
  overflow: hidden;
}

.container {
  height: 800px;
  width: 800px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 50px;
  box-shadow: 0 30px 50px rgba(0, 0, 0, 0.5);
}

#slider {
  width: max-content;
  margin-top: 50px;
}

.item {
  width: 300px;
  height: 400px;
  background-position: 50% 50%;
  display: inline-block;
  background-size: cover;
  position: absolute;
  top: 50%;
  margin-top: -200px;
  border-radius: 20px;
  box-shadow: 0 30px 50px rgba(0, 0, 0, 0.5);
  transition: 0.5s;
}

.item .content {
  width: 300px;
  position: absolute;
  left: 100px;
  top: 50%;
  transform: translateY(-50%);
  font-family: system-ui;
  color: white;
  display: none;
}

.item:nth-child(2) .content {
  display: block;
}

.item .title {
  font-size: 60px;
  font-weight: bold;
  opacity: 0;
  animation: showcontent 1s ease-in-out 1 forwards;
}

.item .des {
  font-size: 30px;
  margin: 30px 0;
  opacity: 0;
  animation: showcontent 1s ease-in-out 0.3s 1 forwards;
}

.item .btn {
  padding: 20px 30px;
  border: none;
  color: #181818;
  border-radius: 10px;
  opacity: 0;
  animation: showcontent 1s ease-in-out 0.6s 1 forwards;
}

/*定义一个动画*/
@keyframes showcontent {
  from {
    opacity: 0;
    transform: translateY(100px);
    filter: blur(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
    filter: blur(0);
  }
}


.item:nth-child(1), .item:nth-child(2) {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  margin-top: 0;
  border-radius: 0;
  box-shadow: none;
}

.item:nth-child(3) {
  left: 50%;
}

.item:nth-child(4) {
  left: calc(50% + 20%);
}

.item:nth-child(5) {
  left: calc(50% + 40%);
}

.item:nth-child(n+6) {
  left: calc(50% + 60%);
  opacity: 0;
}

/*两个按钮*/
.buttons {
  width: 100%;
  position: absolute;
  bottom: 50px;
  margin-left: -50px;
  text-align: center;
}

.s_button {
  display: inline-block;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: #4D7181;
  background-color: white;
  font-size: 25px;
  border-radius: 50%;
  font-weight: bold;
  border: 2px solid #4D7181;
  margin: 0 25px;
  transition: all 0.5s;
}

.s_button:hover {
  background-color: #4D7181;
  color: white;
  cursor: pointer;
}
</style>